<template>
  <div>
    <!-- 车证绑定查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhLicenseBindingQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="exportHandler">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, exportExcel, gettmFiAccountType,exporturl } from '@/api/dealerFinance/FinanceManage/LicenseBindingQuery';
import { exportExcelFile } from '@/utils/index';
import { vehicleConfiguration, getRegion, getNotPage, getFactoryWarehouseList } from '@/api/omFinance';

export default {
  name: 'LicenseBindingQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      exportLoading: false
    };
  },
  components: {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
    // this.getDeptData();
    this.gettmFiAccountType();
    this.getFactoryWarehouseList();
  },
  methods: {
    emitCloseHandler(val, data) {
      this.isDealer = val;
    },
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    // 资金类型
    async gettmFiAccountType() {
      const res = (await gettmFiAccountType({ fundsType: 0, ownerCode: this.dealerCode })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountTypeName').itemList = res;
    },
    // //加载事业部数据
    // async getDeptData() {
    //   const res = await getRegion({});
    //   if (res.resultCode === 200) {
    //     this.topFilterList.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: x.id, text: x.orgName }));
    //   }
    // },
    // 订单细分
    async getNotPage(orderType) {
      let res = await getNotPage({ orderType: orderType, isAvailable: 10041001 });
      if (res.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'subName').itemList = res.data.map(x => ({ text: x.subName, value: x.subId }));
      }
    },
    // 下级工厂
    async getFactoryWarehouseList() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'factoryId').itemList = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 导出
    async exportHandler() {
        let json = Object.assign({}, { CODE: 'CODE_00000234' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;
      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '车证绑定查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'RANGE_DATE',
          label: '分配日期',
          fieldName: 'startMatchDate|endMatchDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6006'),
          change: val => {
            this.topFilterList.find(x => x.fieldName == 'subName').initialValue = '';
            if (val) {
              this.getNotPage(val);
              return;
            }
            this.topFilterList.find(x => x.fieldName == 'subName').itemList = [];
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'subName',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '确认日期',
          fieldName: 'startConfirmDate|endConfirmDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountTypeName',
          placeholder: '请选择',
          itemList: []
        },

        {
          type: 'RANGE_DATE',
          label: '财务审核日期',
          fieldName: 'startFinanceCheckDate|endFinanceCheckDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '下线工厂',
          fieldName: 'factoryId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '发运下达日期',
          fieldName: 'startCommandDate|endCommandDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },

        {
          type: 'INPUT',
          label: '销售订单编号',
          fieldName: 'doNo',
          placeholder: '请输入'
        },

        {
          type: 'INPUT',
          label: '三方票号',
          fieldName: 'billNo',
          placeholder: '请输入'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '事业部',
        //   dataIndex: 'deptName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '三方票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '绑定金额',
          dataIndex: 'chngAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '绑定日期',
          dataIndex: 'accountDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: props => {
            return <span>{this.createDictText(Number(props.row.orderType), '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'subName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6002'),
          render: props => {
            return <span>{this.createDictText(props.row.orderStatus, '6002')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '下线工厂',
          dataIndex: 'factoryName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审核时间',
          dataIndex: 'auditTime',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '分配时间',
          dataIndex: 'matchDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '订单确认时间',
          dataIndex: 'confirmDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '出库日期',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发运下达时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
